/**
 * 
 */
package info.niwota.ezb.web;

import info.niwota.commons.web.PageCursor;
import info.niwota.ezb.Debug;
import info.niwota.ezb.Preferences;
import info.niwota.webapp.acl.CryptoHelper;
import info.niwota.webapp.web.PageContentProvider;
import android.content.Context;
import android.net.Uri;
import android.util.Log;

/**
 * Li, Qiang
 * 
 */
public class WebappPageContentProvider extends PageContentProvider {
	private static final String TAG = "WebappPageContentProvider";
	
	@Override
	protected void showMainPage(PageCursor c, Uri uri, int group, String user,
			String path) throws Exception {
		path = "/bp/index";
		uri = Uri.parse("uri:"+path);
		showContent(c, uri, group, user, path);
	}
	
	@Override
	protected boolean showContent(PageCursor c, Uri uri, int group,
			String user, String path) throws Exception {
		if (Debug.DEBUG) {
			Log.d(TAG, "showContent path: " + path + " uri: " + uri);
		}
		if (path.startsWith("/web/") && path.endsWith(".htmf")) {
			handleAsset(c, path.substring(1), "text/html");
			return true;
		}
		//
		String label = null;
		int labelIdx = 0;
		if (path.startsWith("/bp/")) {
			label = Label.MY_BP;
			labelIdx = 1;
		} 
		
		if (label != null) {
			try {
				checkAccess(uri, group, user, label);
			} catch (Exception e) {
				c.type = PageCursor.Type.CONTENT;
				c.content = e.getLocalizedMessage();
				c.contentLength = c.content.length();
				return true;
			}
		}
		//proceed to generating content for the page
		if (labelIdx == 1) {
			if (path.endsWith("/index")) {
				handleAsset(c, "bp.tpl", "text/html");
				return true;
			}
			if (path.endsWith("/image")) {
				String img = uri.getQueryParameter("uri");
				handleImage(c, img, group, user);
				return true;
			}
			//load /bin/load
			String s = load(c, label, group, user);
			c.type = PageCursor.Type.CONTENT;
			c.contentType = "text/javascript";
			c.content = s;
			c.contentLength = s.length();
		} 
		return true;
	}
	
	@Override
	protected int mapResource(String name) {
		//
		return super.mapResource(name);
	}
	
	private String load(PageCursor c, String label, int group, String user) throws Exception {
		final Context ctx = getContext();
		//load data
		String[] sa = Preferences.getUris(ctx);

		if (sa == null || sa.length == 0) {
			if (Debug.DEBUG) {
				Log.d(TAG, "load: no boarding pass.");
			}
			return "";
		}
		//
		StringBuilder sb = new StringBuilder();
		Uri[] uris = new Uri[sa.length];
		for (int i = 0; i < sa.length; i++) {
			uris[i] = Uri.parse(sa[i]);
		}	
		int size = uris.length;
		//
		sb.append("(function () {\r\nimageCount = " + size + ";\r\n");
		sb.append("imageUris = new Array();\r\n");
		if (size > 0) {
			for (int i = 0; i < size; i++) {
				Uri f = uris[i];
				String p = f.getPath();
				//attach the label to the uri for perm check later when the uri is accessed
				String uri = f.toString();
				String hash = CryptoHelper.hash(p, label, group, user, true);
				String q = "?label="+label+"&hash="+hash;
				//
				sb.append("imageUris[" + i + "]=\"" + uri + q
						+ "\";\r\n");
			}
		}
		sb.append("})();\r\n");
		//
		String s = sb.toString();
		if (Debug.DEBUG) {
			Log.d(TAG, "load: " + s);
		}
		return s;
	}
}
